import Vue from "vue"

import Vuex from "vuex"
import  Cart from "./Cart"
import Product from "./Product"
import timeTravel from "./plugins/timeTravel"
import createPersistedState from "vuex-persistedstate"
Vue.use(Vuex)

export default new Vuex.Store({
    
    state: {
        user: "lbw",
        num: 666,
        products: [],
        msg: "那时一条神奇的天路哟"
    },
    
    mutations: {
    
        INCREMENT_NUM(state, payload) {
            console.log(payload)
            state.num = state.num + payload
        },
        DECREMENT_NUM(state, payload) {
            state.num = state.num - payload
        },
        
        SET_PRODUCT(state, payload) {
            state.products = payload;
        }
    },
   
    actions: {
       
        GET_PRODUCT(context, payload) {
            console.log(context)
            console.log(payload)
            fetch('http://chst.vip/data/index.json')
                .then(body => body.json())
                .then(res => {
                    console.log(res)
                    context.commit("SET_PRODUCT", res.data)

                    // context.state.products = res.data
                })
        }
    },
   
    getters: {
        reverseMsg(state) {
          
            return state.msg.split("").reverse().join("")
        }
    },
 
    modules: {
        Cart,
        Product
    },
    plugins: [timeTravel, createPersistedState({
        storage: window.localStorage,
        key: "2204",
        paths: ['products','user']
    })],
})